package com.liuqinqi.barter.service.impl;

import com.base.dao.service.impl.BaseServiceImpl;
import com.liuqinqi.barter.common.serverResponse.RequestDTO;
import com.liuqinqi.barter.common.serverResponse.ServerResponse;
import com.liuqinqi.barter.exception.ServiceException;
import com.liuqinqi.barter.mapper.MemberMapper;
import com.liuqinqi.barter.model.Member;
import com.liuqinqi.barter.service.MemberService;
import com.liuqinqi.barter.util.MD5Util;
import com.liuqinqi.barter.util.SerialUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class MemberServiceImpl extends BaseServiceImpl<MemberMapper, Member> implements MemberService {

    @Override
    public ServerResponse<String> Test(String str) {
        log.info("str = "+str);
        
        return ServerResponse.createBySuccess(str);
    }

    @Override
    public ServerResponse<Member> login(Member member) {
        return null;
    }

    @Override
    public ServerResponse<Member> register(Member member) {
        //获取账号
        String account = member.getAccount();
        //校验账号是否为空
        RequestDTO.checkRequiredParam(account, "account");
        //获取密码
        String pwd = member.getPwd();
        //校验密码是否为空
        RequestDTO.checkRequiredParam(pwd, "pwd");
        //生成主键Id
        member.setId(SerialUtil.getUid());
        //将密码进行MD5加密
        member.setPwd(MD5Util.MD5EncodeUtf8(member.getPwd()));
        //创建时间
        member.setCreateTime(new Date());
        //将注册信息插入到数据库，成功为true,失败为false
        boolean insertFlag = insert(member);
        if(!insertFlag){
            throw new ServiceException("注册失败");
        }
        //返回member信息前，先将密码信息置空
        member.setPwd(StringUtils.EMPTY);
        return ServerResponse.createBySuccess("注册成功", member);
    }
}
